(function( global,$) {
	var ids = 0; //公共序号
	global.jForest = {
		getId:function(){return (ids++);},
		getAjax:function(options){return new Ajax(options);},
		alert:function(title,msg,click){
			jf.alertFlag=false,click=$.isFunction(click)?click:function(flag){};
			if(!jf.alertModal){jf.alertModal=$('<div class="modal fade" tabindex="-1"><div class="modal-dialog" style="width:400px;">'
				+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
				+'<h4 class="modal-title">'+title+'</h4></div><div class="modal-body">'+msg+'</div>'
				+'<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
				+'<button type="button" class="btn btn-primary">确定</button></div></div></div></div>');
			jf.alertModal.find('.btn-primary').click(function(){jf.alertFlag=true,jf.alertModal.modal('hide');});
			jf.alertModal.on('hidden.bs.modal',function(){click.call(this,jf.alertFlag)});}jf.alertModal.modal('show');},
		error:function(title,msg){
			if(!msg){msg=title,title="系统错误";}
			if(!jf.errorModal){jf.errorModal=$('<div class="modal fade" tabindex="-1"><div class="modal-dialog" style="width:300px;">'
				+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
				+'<h4 class="modal-title">'+title+'</h4></div><div class="modal-body alert alert-danger">'+msg+'</div>'
				+'<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
				+'</div></div></div></div>');}jf.errorModal.modal('show');},
		info:function(title,msg){
			if(!msg){msg=title,title="系统提示";}
			if(!jf.infoModal){jf.infoModal=$('<div class="modal fade" tabindex="-1"><div class="modal-dialog" style="width:300px;">'
				+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
				+'<h4 class="modal-title">'+title+'</h4></div><div class="modal-body alert alert-info">'+msg+'</div>'
				+'<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">取消</button>'
				+'</div></div></div></div>');}jf.infoModal.modal('show');}
	}
	
	var Ajax = function(options){
		this.options = $.extend({
			bindObj:null,	//绑定的对象
			method:'post',
			dataType:'json',
			url:'',
			param:null,  //方法或对象
			callback:function(){}
		},options);
		this.loadedParam = {}; //上一次加载时用的参数
	}
	
	Ajax.prototype = {
		getParam:function(param){
			if($.isFunction(param)){return param.apply(this.options.bindObj||this,[this.loadedParam]);
			}else if(param){return param;} else if($.isFunction(this.options.param)){
				return this.options.param.apply(this.options.bindObj||this,[this.loadedParam]);
			}else{return this.options.param||{};}
		},
		getCallback:function(callback){
			if($.isFunction(callback)){return callback;}
			else if($.isFunction(this.options.callback)){return this.options.callback;}
			else{return function(result){};}
		},
		isValid:function(){return this.options.url?true:false;},
		load:function(param,callback){
			var obj = this.options.bindObj||{},opts=this.options;
			this.loadedParam = this.getParam(param);callback = this.getCallback(callback);
			$.ajax({url:opts.url,data:this.loadedParam,dataType:opts.dataType,type:opts.method,
				success:function(result){callback.apply(obj,[result||{}]);}})
		}
	}
	
	global.jf = global.jForest;
})(typeof window !== "undefined" ? window : this,jQuery);